<?php
// Quick search page.

if (!$_SESSION['usr_id']) {
	// Not logged in!
	// FIXME: It’d be nicer to pop up a login window, then retry the search.
	header("Location: login.php");
}

require_once('config.php');
require_once('functions.php');
require_once('db.php');

function do_query($form, $db, $error) {
	$pid = $form['pid'];
	$item_name = $form['item_name'];
	$lang = $form['lang'];
	$lang_pulldown = $form['lang_pulldown'];
	$country = $form['country'];
	$country_pulldown = $form['country_pulldown'];

	// If the user didn’t enter anything in the country field then we get
	// the filler text which we’ll ignore.
	if ($country == $qsearch_cty_dflt)
		$country = null;

	// Sanity checking.
	if ($country_pulldown != "") {
		$len = strlen($country_pulldown);
		// Country codes are only 2 or 3 chars long.
		if ($len < 2 && $len > 3) {
			$error = "Can’t happen: invalid country selected!";
			return false;
		}
	} 

	$query_values = array('item_pid' => $pid,
			      'item_name' => $item_name,
			      'item_source_language' => $lang,
			      'item_countries' => $country);

	// Build the query string.
	$query = "SELECT * FROM items ";
	$query .= mysql_build_where_clause($query_values);

	// Query the database.
	$rs = mysql_query($query, $db);
	if (!$rs) die(mysql_errno_error());

	// Return the search results.
	return $rs;
}

// Load smarty.
require($smarty_source);
$smarty = new Smarty();
$smarty->template_dir = $smarty_tpl;
$smarty->compile_dir = $smarty_compile_dir;
$smarty->config_dir = $smarty_config_dir;
$smarty->cache_dir = $smarty_cache_dir;
$smarty->debugging = $smarty_dbg;

// Copy the default form values. They’re defined in config.php.
$smarty->pid_default = $qsearch_pid_dflt;
$smarty->item_default = $qsearch_item_dflt;
$smarty->lang_default = $qsearch_lang_dflt;
$smarty->country_default = $qsearch_cty_default;

$db_conn = mysql_connect_localhost_readonly($larch_db_name);

$error = "";

//
// Prepare the country_pulldown and lang_pulldown form elements.
//

// FIXME: Maybe only offer the countries and languages actually represented
// in the database, rather than all possible languages and countries?

// Populate the $countries array from the ISO_3166 table.
$result = mysql_query("SELECT alpha2, name FROM ISO_3166 ORDER BY name;",
	 		$db_conn);
if ($result == false) {
	// FIXME: Handle error.
} elseif (mysql_num_rows($result) == 0) {
	// FIXME: Handle error.
} else {
	$countries = array();
	while ($row = mysql_fetch_assoc($result)) {
		$countries[$row['alpha2']] = $row['name'];
	}
}

// Now stick the $countries array into smarty so it can be used for the
// country_pulldown form element.
$smarty->assign('country_pulldown_data', $countries);

// Lather, rinse, and repeat for $languages from the ISO_639_3 table.
$result = mysql_query("SELECT id, ref_name FROM ISO_639_3 ORDER BY name;",
			$db_conn);
if ($result == false) {
	// FIXME: Handle error.
} elseif (mysql_num_rows($result) == 0) {
	// FIXME: Handle error.
} else {
	$languages = array();
	while ($row = mysql_fetch_assoc($result)) {
		$languages[$row['id']] = $row['ref_name'];
	}
}

// Stick $languages into smarty.
$smarty->assign('lang_pulldown_data', $languages);

//
// Handle actual searches.
//

// If the URL includes any search parameters, run the search.
if ($_GET and (isset($_GET['pid'])
		or isset($_GET['item_name'])
		or isset($_GET['lang'])
		or isset($_GET['lang_pulldown'])
		or isset($_GET['country'])
		or isset($_GET['country_pulldown'])) {
	// Database connection. Only read-only needed.
	$db_conn = mysql_connect_localhost_readonly($larch_db_name);

	// Do the query.
	$error = "";
	$result = do_query($_GET, $db_conn);

	// Error handling.
	if ($result == false) {
		// Do something?
		$result;
	}
	
	// If there are no results then say so.
	if (mysql_num_rows($result) == 0) {
		$smarty->assign('no_results', 'true')
	} else {
		$result;
	}

	// Display results.
}

$smarty->assign('larch_name', $larch_name);
$smarty->assign('larch_name_subtitle', $larch_name_subtitle);
$smarty->assign('session_username', $_SESSION['usr_username']);

$smarty->display('qsearch.tpl');
?>
